package com.kiln.juxingqs.adapter

import android.content.Context
import android.content.Intent
import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.recyclerview.widget.DiffUtil
import androidx.recyclerview.widget.RecyclerView
import client.global.commlib.base.BaseLoadmoreAdapter
import client.global.commlib.base.BindingViewHoder
import client.global.commlib.network.HhRetroHttp
import client.global.commlib.toast.toast
import com.kiln.juxingqs.R
import com.kiln.juxingqs.api.bean.ArticleInfo
import com.kiln.juxingqs.api.bean.GoodsIndex
import com.kiln.juxingqs.databinding.LayoutPtzxitemBinding
import com.kiln.juxingqs.databinding.LayoutZrppitemBinding
import com.kiln.juxingqs.ui.PtzxDetailActivity
import com.kiln.juxingqs.ui.WebActivity


class PtzxAdapter : BaseLoadmoreAdapter<ArticleInfo.Article> {

    lateinit var goodsIndex: GoodsIndex

    constructor(context: Context) : super(context,
        object : DiffUtil.ItemCallback<ArticleInfo.Article>() {
            override fun areItemsTheSame(
                oldItem: ArticleInfo.Article,
                newItem: ArticleInfo.Article
            ): Boolean {
                return false
            }

            override fun areContentsTheSame(
                oldItem: ArticleInfo.Article,
                newItem: ArticleInfo.Article
            ): Boolean {
                return false
            }
        }) {
    }

    override fun createVh(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
        return BindingViewHoder(
            DataBindingUtil.inflate(
                layoutInflater,
                R.layout.layout_ptzxitem,
                parent,
                false
            )
        )
    }

    fun submitData(list: MutableList<ArticleInfo.Article?>) {
        super.submitList(list)
    }

    override fun bindVh(holder: RecyclerView.ViewHolder, position: Int) {
        val bindingViewHolder = holder as BindingViewHoder<ViewDataBinding>
        val binding = bindingViewHolder.binding as LayoutPtzxitemBinding
        val bean = getItem(position)
        binding.index = position
        binding.bean = bean
        binding.event = this
//                binding.index = "$position"
//                binding.event = this
//                manager.load(bean.goods_img)
//                    .into(binding.img)

    }

    fun itemClick(bean: ArticleInfo.Article, index: Int) {
        if (itemCount - 1 == index + 1) {
            context.startActivity(Intent(context, WebActivity::class.java).apply {
                putExtra("adInfo", bean.url)
                putExtra("title", bean.title)
            })

        } else
            context.startActivity(Intent(context, PtzxDetailActivity::class.java).apply {
                putExtra("bean", bean)
            })
    }

}